package com.du.dupload;

import java.awt.Cursor;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;

public class SwingStatusButton extends JButton {
	enum State {
		STOP, START, FINISH
	}

	private State state = State.START;
	private IUICallbackHandler ch;
	private static ImageIcon iPlay = new ImageIcon(Toolkit.getDefaultToolkit()
			.getImage(SwingContainer.class.getResource("/res/Play.png")));
	private static ImageIcon iStop = new ImageIcon(Toolkit.getDefaultToolkit()
			.getImage(SwingContainer.class.getResource("/res/Stop.png")));
	
	private static ImageIcon iFinish = new ImageIcon(Toolkit.getDefaultToolkit()
			.getImage(SwingContainer.class.getResource("/res/flag_finish.png")));


	SwingStatusButton() {
		super(iPlay);
		//setBorder(BorderFactory.createEmptyBorder());
		setCursor(new Cursor(Cursor.HAND_CURSOR));
		setMargin(new Insets(0, 0, 0, 0));
		// to add a different background
		// to remove the border
		setBorder(null);
		setBorderPainted(false);
	    setContentAreaFilled(false);
	    setFocusPainted(false);

	}

	public void changeState(State st) {
		this.state = st;
		switch (state) {
		case STOP:
			setIcon(iPlay);
			//ch.terminateCurrentTask();
			break;
		case START:
			setIcon(iStop);
			//ch.continueTask();
			break;
		case FINISH:
			setIcon(iFinish);
			//ch.continueTask();
			break;

		}
	}

	public void setHandler(IUICallbackHandler ch) {
		this.ch = ch;
		addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				switch (state) {
				case STOP:
					//setIcon(iStop);
					//state = State.START;
					if (ch != null) {
						ch.continueTask();
					}
					break;
				case START:
					//setIcon(iPlay);
					//state = State.STOP;
					if (ch != null) {
						ch.terminateCurrentTask();
					}
					break;

				}
			}
		});
	}
}
